package com.kt.mes.service.impl;

import java.util.ArrayList;
import java.util.List;

import com.kt.mes.domain.ProcessRoute;
import com.kt.mes.domain.ProcessRouteItemParam;
import com.kt.mes.domain.TreeSelectPro;
import com.kt.mes.mapper.ProcessRouteItemParamMapper;
import com.kt.mes.mapper.ProcessRouteMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.kt.mes.mapper.ProcessRouteItemMapper;
import com.kt.mes.domain.ProcessRouteItem;
import com.kt.mes.service.IProcessRouteItemService;
import org.springframework.transaction.annotation.Transactional;

/**
 * 工艺路线详情Service业务层处理
 * 
 * @author lzc
 * @date 2021-01-18
 */
@SuppressWarnings("ALL")
@Service
public class ProcessRouteItemServiceImpl implements IProcessRouteItemService 
{
    @Autowired
    private ProcessRouteItemMapper processRouteItemMapper;

    @Autowired
    private ProcessRouteMapper processRouteMapper;

    @Autowired
    private ProcessRouteItemParamMapper processRouteItemParamMapper;
    /**
     * 查询工艺路线详情
     * 
     * @param id 工艺路线详情ID
     * @return 工艺路线详情
     */
    @Override
    public ProcessRouteItem selectProcessRouteItemById(Long id)
    {
        return processRouteItemMapper.selectProcessRouteItemById(id);
    }

    /**
     * 查询工艺路线详情列表
     * 
     * @param processRouteItem 工艺路线详情
     * @return 工艺路线详情
     */
    @Override
    public List<ProcessRouteItem> selectProcessRouteItemList(ProcessRouteItem processRouteItem)
    {
        return processRouteItemMapper.selectProcessRouteItemList(processRouteItem);
    }

    /**
     * 新增工艺路线详情
     * 
     * @param processRouteItem 工艺路线详情
     * @return 结果
     */
    @Override
    @Transactional
    public int insertProcessRouteItem(ProcessRouteItem processRouteItem)
    {

        //先添加内容
        int flag=0;
        flag=processRouteItemMapper.insertProcessRouteItem(processRouteItem);

        processRouteItem.getParamList().forEach(item->{
            item.setTpriId(processRouteItem.getId());
             processRouteItemParamMapper.insertProcessRouteItemParam(item);
        });
        //再添加参数


        return flag;
    }

    /**
     * 修改工艺路线详情
     * 
     * @param processRouteItem 工艺路线详情
     * @return 结果
     */
    @Override
    public int updateProcessRouteItem(ProcessRouteItem processRouteItem)
    {
        //修改参数
        processRouteItem.getParamList().forEach(v->{
            processRouteItemParamMapper.updateProcessRouteItemParam(v);
        });

        return processRouteItemMapper.updateProcessRouteItem(processRouteItem);
    }

    /**
     * 批量删除工艺路线详情
     * 
     * @param ids 需要删除的工艺路线详情ID
     * @return 结果
     */
    @Override
    public int deleteProcessRouteItemByIds(Long[] ids)
    {
        return processRouteItemMapper.deleteProcessRouteItemByIds(ids);
    }

    /**
     * 删除工艺路线详情信息
     * 
     * @param id 工艺路线详情ID
     * @return 结果
     */
    @Override
    public int deleteProcessRouteItemById(Long id)
    {
        return processRouteItemMapper.deleteProcessRouteItemById(id);
    }



    @Override
    public List<TreeSelectPro> buildProcessItemTreeSelect(String prId) {

        List<TreeSelectPro>  returnList=new ArrayList<>();

        //根据 prId 查询 工序
        ProcessRoute query=new ProcessRoute();
        query.setPrId(prId);
        List<ProcessRoute> list_pro=  processRouteMapper.selectProcessRouteList(query);
        //父节点
        TreeSelectPro  rootTree =new TreeSelectPro();
        if(list_pro.isEmpty()==false && list_pro.size()==1){
            rootTree.setLv("1");
            rootTree.setId(prId);
            rootTree.setLabel(prId+"-"+list_pro.get(0).getPrName());

        }

        ProcessRouteItem queryItem=new ProcessRouteItem();
        queryItem.setPrId(prId);
        List<ProcessRouteItem> list_proItem=  processRouteItemMapper.selectProcessRouteItemList(queryItem);

        if(list_proItem.isEmpty()==false){
            List<TreeSelectPro> sonList=new ArrayList<>();
          for(ProcessRouteItem temp:list_proItem){
              TreeSelectPro sonTree=new TreeSelectPro();
              sonTree.setLv("2");
              sonTree.setId(temp.getId()+"");
              sonTree.setLabel(temp.getPriNum()+" > "+temp.getPriName());
              sonList.add(sonTree);
          }
            rootTree.setChildren(sonList);

        }
        returnList.add(rootTree);

        return returnList;
    }
}
